/*
Author: Surpass-HC
Source: https://gitee.com/hxchuan000/clpc-1
Mail: surpass-hc@qq.com
*/
#include <clptcFunItf.h>
#include <clptcDlgItf.h>
#include <clptcParse.h>
#include <clptcCmdList.h>
#include <queBase.h>
#include <genrand.h>
#include <parmNorm.h>
#include <queObj.h>

int clptcFunItfInit(clptcFunItfStc *funptr)
{
    if (!(funptr))
    {
        logdebugc("");
        return -1;
    }
    funptr->lpDlgMgr = NULL;
    funptr->lpPkrPsr = NULL;
    funptr->lpType = clptcFunType_Sub;
// #if CLPTC_CPL_HOST_ENABLE
//     funptr->cmdDlg = NULL;
// #endif
#if DEVICE_PLATFORM_CONFIGURE == DEVICE_PLATFORM_LINUX
    system("mkdir -p " CLPTC_CHK_FILE_SAVE_DIR);
    // shmOptInit(&funptr->hostRcv, CLPTC_SHM_HOST_RCV_KEY, 60 * 1024, 1, CLPTC_CHK_FILE_SAVE_DIR "lpHostRcvShmLock.txt");
    shmOptInit(&funptr->hostSnd, CLPTC_SHM_HOST_SND_KEY, 64 * 1024, 256, CLPTC_CHK_FILE_SAVE_DIR "lpHostSndShmLock.txt");
    msgOptInit(&funptr->hostRcv, CLPTC_SHM_HOST_RCV_KEY, 1);
#endif
    return 0;
}
int clptcFunCfgDlgMgr(clptcFunItfStc *funptr, void *cfgDlgMgr, uint8_t cfgDnbr, int cfgtype)
{
    if (!(funptr && cfgDlgMgr))
    {
        logdebugc("");
        return -1;
    }
    funptr->lpDlgMgr = cfgDlgMgr;
    funptr->lpType = cfgtype;
    clptcDlgMgrInit(funptr->lpDlgMgr, funptr, cfgDnbr);
    logdebugc("cfg mgr :type=%d,addr=%d", funptr->lpType, parmNormIstc.addr485);
#if DEVICE_PLATFORM_CONFIGURE == DEVICE_PLATFORM_LINUX
    if (funptr->lpType == clptcFunType_Sub)
    {
        // clean sub receive que.
        shmOptRestRcv(&funptr->hostSnd, parmNormIstc.addr485);
    }
    else
    {
        // clean host receive que.
        // shmOptRestRcv(&funptr->hostRcv, 0);
        msgOptClean(&funptr->hostRcv, 0);
    }
#endif
    return 0;
}
int clptcFunCfgPkrPsr(clptcFunItfStc *funptr, void *cfgPkrPsr, int cfgPsize)
{
    if (!(funptr && cfgPkrPsr))
    {
        logdebugc("");
        return -1;
    }
    funptr->lpPkrPsr = cfgPkrPsr;
    clptcInitV1(funptr->lpPkrPsr, cfgPsize, funptr);
    return 0;
}
int clptcFunGetType(clptcFunItfStc *funptr)
{
    if (!(funptr))
    {
        logdebugc("");
        return -1;
    }
    return funptr->lpType;
}
int clptcFunItfRcv(clptcFunItfStc *funptr, uint8_t *optbuf, int optlen)
{
    if (!(funptr))
    {
        logdebugc("");
        return 0;
    }
#if DEVICE_PLATFORM_CONFIGURE == DEVICE_PLATFORM_LINUX
    if (funptr->lpType == clptcFunType_Host)
    {
        struct msgDtStct tmpdata;
        int tmplen = msgOptRcv(&funptr->hostRcv, &tmpdata);
        if (tmplen > 0)
        {
            queOptWrite(funptr->rcvQue, (const uint8_t *)tmpdata.msgData, (queOptType)tmpdata.msgSize);
        }
        return queOptRead(funptr->rcvQue, 0, optbuf, optlen);
    }
    else
    {
        return shmOptRead(&funptr->hostSnd, parmNormIstc.addr485, optbuf, optlen);
    }
#else
    return 0;
#endif
}
int clptcFunItfSnd(clptcFunItfStc *funptr, const uint8_t *optbuf, int optlen)
{
    if (!(funptr))
    {
        logdebugc("");
        return -1;
    }
#if DEVICE_PLATFORM_CONFIGURE == DEVICE_PLATFORM_LINUX
    if (funptr->lpType == clptcFunType_Host)
    {
        return shmOptWrite(&funptr->hostSnd, optbuf, optlen);
    }
    else
    {
        // return shmOptWrite(&funptr->hostRcv, optbuf, optlen);
        return msgOptSendMem(&funptr->hostRcv, optbuf, optlen, 1, 0);
    }
#else
    return 0;
#endif
}

int clptcFunItfUpdate(clptcFunItfStc *funptr)
{
// #if CLPTC_CPL_HOST_ENABLE
//     clptcFunItfDoComCmd(funptr);
// #endif
    clptcDlgMgrUpdate(funptr->lpDlgMgr);
    return 0;
}

#if CLPTC_CPL_HOST_ENABLE
int clptcFunCfgRcvQue(clptcFunItfStc *funptr, void *cfgRcvQue, int qlen)
{
    if (!(funptr && cfgRcvQue))
    {
        logdebugc("");
        return -1;
    }
    funptr->rcvQue = cfgRcvQue;
    queOptInit(funptr->rcvQue, qlen, 1);
    return 0;
}
#endif
